package com.stylefeng.guns.modular.system.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.stylefeng.guns.common.persistence.dao.AreaMapper;
import com.stylefeng.guns.common.persistence.model.Area;
import com.stylefeng.guns.core.node.ZTreeNode;
import com.stylefeng.guns.modular.system.service.IAreaService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 省市及手续费配置表 服务实现类
 * </p>
 *
 * @author stylefeng123
 * @since 2018-01-20
 */
@Service
public class AreaServiceImpl extends ServiceImpl<AreaMapper, Area> implements IAreaService {
    @Autowired
    private AreaMapper areaMapper;

    @Override
    public List<ZTreeNode> tree() {
        return areaMapper.tree();
    }

    @Override
    public List<ZTreeNode> areatree() {
        return areaMapper.areatree();
    }


    /**
     * 查询区域 省/市
     *
     * @return
     */
    @Override
    public List<Map<String, Object>> selectArea(String pcode) {
        return areaMapper.selectArea(pcode);
    }

    /**
     * 通过code显示省市
     * @param code
     * @return
     */
    @Override
    public Area selectByCode(String code) {
        return areaMapper.selectByCode(code);
    }


    List<Area> areaList = new ArrayList<Area>();

    @PostConstruct
    public void localAreaList() {
        List<Area> tmpAreas = areaMapper.selectAll();
        if (CollectionUtils.isNotEmpty(tmpAreas)) {
            if (CollectionUtils.isNotEmpty(areaList)) {
                areaList.clear();
            }
            areaList.addAll(tmpAreas);
        }
    }


    @Override
    public List<Map<String, Object>> selectPage(Page page, String condition) {
        return areaMapper.selectPage(page, condition);
    }

    /**
     * 查询省份
     *
     * @param name
     * @return
     */
    @Override
    public Area selectName(String name) {
        return areaMapper.selectName(name);
    }

    /**
     * 通过省查询市
     *
     * @return
     */
    @Override
    public List<Map<String, Object>> selectSQ(Page<Area> page, String pcode,String name) {
        return areaMapper.selectSQ(page, pcode,name);
    }

    /**
     * 对应前端的省市联动
     */
    @Override
    public List<Area> selectProCity(String pcode) {
        Map<String, List<Area>> areaMap = areaList.stream().collect(Collectors.groupingBy(Area::getPcode));
        List<Area> areas = areaMap.get(pcode);
        return areas;
    }

    @Override
    public String selectNameByCode(String code) {
        Area area = areaMapper.selectByCode(code);
        if (area == null) {
            return StringUtils.EMPTY;
        }
        return area.getName();

    }

    @Override
    public List<Area> selectPcode(@Param("pcodename") String pcodename) {
        Map<String, List<Area>> areaMap = areaList.stream().collect(Collectors.groupingBy(Area::getName));
        List<Area> areas = areaMap.get(pcodename);
        return areas;
    }


    @Override
    public List<Area> selectAll() {
        if (CollectionUtils.isNotEmpty(areaList)) {
            return areaList;
        }
        return areaMapper.selectAll();
    }

    @Override
    public String selectNameByCacheCode(String code) {
        if (StringUtils.isBlank(code)) {
            return StringUtils.EMPTY;
        }
        Area area = areaList.stream().filter(data -> StringUtils.equals(data.getCode(), code)).findAny().orElse(null);
        if (area == null) {
            return StringUtils.EMPTY;
        }
        return area.getName();
    }

    @Override
    public void refreshAreaCache() {
        localAreaList();
    }



}
